#ifndef PINPOINTHANDLE_H
#define PINPOINTHANDLE_H

#include <QDebug>
#include <QPointF>
#include <QHash>
#include <QString>
#include <stdexcept>
#include <QRectF>
#include <QString>
#include <QHash>
#include <QGraphicsItem>
#include "globaldata.h"

class QGraphicsEllipseItem;
class GraphicsRectItem;

const int EllipseItemRadius = 10;

enum class Direction {
    None,
    Right,
    Down,
    Left,
    Up,
};

class DirectionConverter {
public:
    static Direction toDirection(const QString& dirStr);
    static QString toString(Direction dir);
};


class PinPointHandle : public QGraphicsEllipseItem
{
public:
    PinPointHandle( const Pin& pin, QGraphicsItem *parent = nullptr)
        :QGraphicsEllipseItem(parent)
        ,m_pin(pin)
    {}
    ~PinPointHandle()
    {}
    const Pin& getPin() const { return m_pin; }
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    void move(qreal x, qreal y );
    QPointF getPos() const {return m_pos;}
    QRectF getParentRect() const;

    bool contains(const QPointF &point) const override;

private:
    Pin m_pin;
    QPointF m_pos;
};

class PinPointHandleGroup
{
public:
    explicit PinPointHandleGroup(const Unit& unit, QGraphicsItem* parent);
    ~PinPointHandleGroup();

    void addPinByDirection(const Pin& pin, QGraphicsItem* parent);
    void movePins(Direction dir, QRectF rect);
    PinPointHandle *getClickPin(const QPointF& pos);
    PinPointHandle *getPinFormPinIndex(int index);

    const QList<PinPointHandle*>& getPins(Direction dir) const;
    const QList<PinPointHandle*>& getLeftPins() const { return leftPins; }
    const QList<PinPointHandle*>& getRightPins() const { return rightPins; }
    const QList<PinPointHandle*>& getUpPins() const { return upPins; }
    const QList<PinPointHandle*>& getDownPins() const { return downPins; }

    void updatePinPointHandle(GraphicsRectItem* item);

private:
    QList<PinPointHandle*> leftPins;    // 左方向引脚
    QList<PinPointHandle*> rightPins;   // 右方向引脚
    QList<PinPointHandle*> upPins;      // 上方向引脚
    QList<PinPointHandle*> downPins;    // 下方向引脚
};

#endif // PINPOINTHANDLE_H
